
;
(function($, $$) {

    $.fn.accordion = function(options) {
        var self = this, args = Array.prototype.slice.call(arguments);
        args.push($$.Accordion);
        var result = $$.createWidgetByJQuery.apply(self, args);
        if (result.jqObj !== undefined && options !== "widget") {
            return self;
        }
        return result;
    };

    $$.Accordion = function(self, options) {
        if (!(this instanceof $$.Accordion)) {
            return new $$.Accordion(self, options);
        }
        var defaults = {

            accordionClass : $$.defaultBorder + " uiCornerAll " + $$.defaultBoxShadow,

            titleBarClass : $$.defaultGradientBG,

            activeClass : $$.defaultGradientBG,

            itemTitleBarClass : "uiBlackGradientBG",

            active : [0],

            event : "click",

            titleIcon : null,

            animation : "toggle",

            title : null,

            itemTitleList : null,

            height : "auto",

            toggle : false,

            hideOthers : true,

            animating : false,

            titleBarHTML : '<div class="uiAccordionTitleBar uiNoSelectText"><span></span></div>',

            itemTitleBarHTML : '<div class="uiTitleBar uiNoSelectText"><span class="uiUserBtn uiSmallIcon uiArrowDownIcon"></span><span class="uiTitle"></span></div>',

            changeStart : null,

            change : null
        };
        var opts = $.extend(defaults, options);
        $$.Accordion.prototype.superClass.call(this, self, opts);
        this.init();
    };
    $$.inherit($$.Accordion, $$.Widget, {

        init : function() {
            var accordionObj = this;
            accordionObj.createWidget();
            initAccordion(accordionObj.jqObj, accordionObj.opts);
            return accordionObj;
        },

        activate : function(index) {
            var accordionObj = this, self = accordionObj.jqObj, opts = accordionObj.opts;
            if (index === undefined) {
                var activateArr = [];
                var titleBarList = $("> .uiTitleBar", self);
                titleBarList.each(function(n) {
                    if ($(this).hasClass(opts.activeClass)) {
                        activateArr.push(n);
                    }
                });
                return activateArr;
            }
            var obj = $("> .uiTitleBar", self).eq(index);
            if (!obj.hasClass(opts.activeClass)) {
                obj.trigger(opts.event);
            }
            return accordionObj;
        },

        item : function(index, content, title) {
            var accordionObj = this, self = accordionObj.jqObj, opts = accordionObj.opts;
            index = index || 0;
            var titleBar = $("> .uiTitleBar", self).eq(index);
            var contentObj = titleBar.next();
            if (content === undefined) {
                return titleBar.next();
            }
            if (title === undefined) {
                if (content !== null) {
                    contentObj.html(content);
                    return accordionObj;
                }
                return titleBar;
            }
            titleBar.children(".uiTitle").html(title);
            if (content !== null) {
                contentObj.html(content);
                return accordionObj;
            }
        },

        addItem : function(item, title, index) {
            var accordionObj = this, self = accordionObj.jqObj, opts = accordionObj.opts, itemObj = $(item), title = title || itemObj.attr("title");
            var titleBar = $(opts.itemTitleBarHTML).addClass(opts.itemTitleBarClass)
                .children(".uiTitle").html(title).end();
            itemObj.addClass("uiContent uiHidden").height(opts.height);
            if (index !== undefined) {
                var obj = $("> .uiTitleBar", self).eq(index);
                titleBar.insertBefore(obj);
                itemObj.insertBefore(obj);
            }
            else {
                self.append(titleBar).append(itemObj);
            }
            return accordionObj;
        },

        removeItem : function(index) {
            var accordionObj = this, self = accordionObj.jqObj, opts = accordionObj.opts;
            index = index || 0;
            return $("> .uiTitleBar", self).eq(index).next().andSelf().remove();
        },

        title : function(title) {
            var accordionObj = this, self = accordionObj.jqObj, opts = accordionObj.opts;
            var obj = $("> .uiAccordionTitleBar > span:last", self);
            if (title === undefined) {
                return obj.text();
            }
            obj.text(title);
            return accordionObj;
        },

        titleIcon : function(titleIconClass) {
            var accordionObj = this, self = accordionObj.jqObj, opts = accordionObj.opts;
            if (titleIconClass === undefined) {
                return opts.titleIcon;
            }
            if (opts.titleIcon === null) {
                $("> .uiAccordionTitleBar", self).prepend($('<span class="uiTitleIcon" />'));
            }
            var obj = $("> .uiAccordionTitleBar > span.uiTitleIcon", self).removeClass(opts.titleIcon).addClass(titleIconClass);
            opts.titleIcon = titleIconClass;
            return accordionObj;
        }
    });

    function initAccordion(self, opts) {
        var titleBar = null, title = opts.title || self.attr("title");

        if (title) {
            titleBar = $(opts.titleBarHTML).addClass(opts.titleBarClass).children("span").html(title).end();
            if (opts.titleIcon) {
                titleBar.prepend($('<span class="uiTitleIcon" />').addClass(opts.titleIcon));
            }
        }

        self.addClass("uiAccordion uiWidget " + opts.accordionClass).find("> div").each(
            function(n) {
                var contentClass = "uiHidden", titleBarClass = opts.itemTitleBarClass, buttonClass = "", obj = $(this);

                if (opts.active === "all" || $.inArray(n, opts.active) !== -1) {
                    contentClass = "";
                    titleBarClass = opts.activeClass + " uiActive";
                    buttonClass = "uiArrowUpIcon uiArrowDownIcon";
                }

                title = null;
                if ($.isArray(opts.itemTitleList)) {
                    title = opts.itemTitleList[n];
                }
                title = title || obj.attr("title");
                $(opts.itemTitleBarHTML).addClass(titleBarClass)
                    .children(".uiUserBtn").toggleClass(buttonClass)
                    .siblings(".uiTitle").html(title).end().end()
                    .insertBefore(obj.addClass("uiContent " + contentClass).height(opts.height));
            }).end()
            .prepend(titleBar);


        initEvent(self, opts);
        if (opts.disabled) {
        }
    }


    function initEvent(self, opts) {

        self.bind(opts.event + ".uiAccordion", function(e) {
            if (opts.disabled || opts.animating) {
                return;
            }
            var target = $(e.target);
            if (!target.hasClass("uiTitleBar")) {
                target = target.parent(".uiTitleBar");
                if (target.length == 0) {
                    return;
                }
            }
            if (!opts.toggle) {
                if (target.hasClass(opts.activeClass)) {
                    return;
                }
            }
            if ($.isFunction(opts.changeStart)) {
                if (opts.changeStart.call(self[0], target, e) === false) {
                    return self;
                }
            }
            opts.animating = true;
            var selectedList = opts.hideOthers === true ? $("> .uiTitleBar." + opts.activeClass, self).not(target).removeClass(opts.activeClass + " uiActive").addClass(opts.itemTitleBarClass) : null;
            target.toggleClass(opts.itemTitleBarClass).toggleClass(opts.activeClass + " uiActive").add(selectedList)
                .children(".uiUserBtn").toggleClass("uiArrowUpIcon uiArrowDownIcon").end()
                .next(".uiContent").stop(true, true)[opts.animation](opts.animateTime, function() {
                if ($(this).is(":visible")) {
                    if ($.isFunction(opts.change)) {
                        opts.change.call(self[0], target, e);
                    }
                }
                opts.animating = false;
            });
        });
    }
})(jQuery, BASE);

